﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

using Serilog;

namespace Tszy.Unlimited.Base.Infrastructure.Logging;

internal static class LoggerExtensions
{
    public static IServiceCollection RegisterLogger(this IServiceCollection services, IConfiguration configuration)
    {
        var logger = new LoggerConfiguration()
            //.WriteTo.Console()
            .ReadFrom
            .Configuration(configuration)
            .CreateLogger();

        Log.Logger = logger;

        return services
            .RemoveAll(typeof(ILogger))
            .AddSingleton<ILogger, SerilogLogger>();
    }

    public static IServiceCollection RegisterLogger(this IServiceCollection services, ILogger logger)
    {
        return services
            .RemoveAll(typeof(ILogger))
            .AddSingleton(logger);
    }
}